package com.qianbei.common.view.title;

import android.app.Activity;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.qianbei.R;
import com.qianbei.common.base.BaseFragment;

public class TitleFragment extends BaseFragment {

	private TextView title_left, title_left2;
	public TextView title_center;
	private TextView tv_right;
	private ImageView title_right, title_right2,title_left_img,img_left3;
	private String titleString;
	private ImageView mPoint1;
	private ImageView mPoint2;
	private ImageView mPoint3;
	private ImageView mRedPoint;

	private View v;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TypedArray a = getActivity().obtainStyledAttributes(attrs,
		// R.styleable.titlebar);
		// titleString = a.getString(R.styleable.titlebar_titleString);
		// a.recycle();
		if (v == null){
			v = inflater.inflate(R.layout.comm_title_fragment, container, false);
		}

		ViewGroup parent = (ViewGroup) v.getParent();
		if (parent != null)
		{
			parent.removeView(v);
		}


		title_center = (TextView) v.findViewById(R.id.title_center);
		title_right = (ImageView) v.findViewById(R.id.title_right);
		title_right2 =(ImageView) v.findViewById(R.id.title_right2);
		tv_right = (TextView) v.findViewById(R.id.tv_right);
		title_left = (TextView) v.findViewById(R.id.title_left);
		title_left2 = (TextView) v.findViewById(R.id.title_left2);
		img_left3 = (ImageView) v.findViewById(R.id.img_left3);
		mPoint1 = (ImageView) v.findViewById(R.id.image_point_1);
		mPoint2 = (ImageView) v.findViewById(R.id.image_point_2);
		mPoint3 = (ImageView) v.findViewById(R.id.image_point_3);
		mRedPoint = (ImageView) v.findViewById(R.id.iv_red_point);

		
		title_left_img = (ImageView) v.findViewById(R.id.title_left_img);
		title_left.setOnClickListener(this);
		title_left2.setOnClickListener(this);
		title_right.setOnClickListener(this);
		title_right2.setOnClickListener(this);
		tv_right.setOnClickListener(this);
		title_center.setOnClickListener(this);
		title_center.setText(titleString);
		return v;
	}
	
	public void setRoll(){
		img_left3.startAnimation(
				AnimationUtils.loadAnimation(mContext,
						R.anim.anim_title_imge3_progress));
	}

	/**
	 * 绑定自定义 属性
	 */
	@Override
	public void onInflate(Activity activity, AttributeSet attrs,
			Bundle savedInstanceState) {
		super.onInflate(activity, attrs, savedInstanceState);
		TypedArray a = activity.obtainStyledAttributes(attrs,
				R.styleable.title_text);
		titleString = a.getString(R.styleable.title_text_titleString);
		a.recycle();
		

	}

	// 编辑标题
	public void setTitle(String str) {
		title_center.setText(str);
	}

	public void setTiltLength(int length){
		title_center.setMaxLines(length);
	}

	public String getTitle() {
		return title_center.getText().toString();
	}

	// 编辑标题按钮图片
	public void setCentterImg(int resId) {
		Drawable nav_up = getResources().getDrawable(resId);
		nav_up.setBounds(0, 5, nav_up.getMinimumWidth(),
				nav_up.getMinimumHeight());
		title_center.setCompoundDrawables(null, null, nav_up, null);
	}


	/** ----------编辑左边部分------------ **/

	// 编辑返回按钮图片
	public void setLeftImg(int resId) {
		Drawable nav_up = getResources().getDrawable(resId);
		nav_up.setBounds(0, 0, nav_up.getMinimumWidth(),
				nav_up.getMinimumHeight());
		title_left.setCompoundDrawables(nav_up, null, null, null);
	}

	// 设置左边 文字
	public void setLfetText(String text) {
		title_left.setText(text);
	}

	public void setImageLeft(int img) {
		title_left_img.setVisibility(View.VISIBLE);
		title_left_img.setImageResource(img);
	}
	public  void setLefeGone(){
		title_left.setVisibility(View.GONE);
	}

	public void setLeft2Text(String text) {
		title_left2.setVisibility(View.VISIBLE);
		title_left2.setText(text);
	}
	
	public void setLeft3Image(int img){
		img_left3.setImageResource( img);
	}

	public void setLeftImageInVisable() {
		title_left.setVisibility(View.GONE);
	}

	public void setLeftImageVisibility(int visibility) {
		title_left.setVisibility(visibility);
	}

	/** -----------编辑右边部分------------------ */

	// 编辑按钮
	public void setRightImage(int resId) {
		title_right.setVisibility(View.VISIBLE);
		title_right.setImageResource(resId);
	}
	public void setRightImage2(int resId) {
		title_right2.setVisibility(View.VISIBLE);
		title_right2.setImageResource(resId);
	}
	

	public void setRightImgGone() {
		title_right.setVisibility(View.GONE);
	}

	/**
	 * 右边视图设置视图可见性
	 * 
	 * @param visibility
	 */
	public void setRightbuttonVisiable(int visibility) {
		title_right.setVisibility(visibility);
	}

	public void setrightTitle(String str) {
		// title_right.setVisibility(View.GONE);
		tv_right.setVisibility(View.VISIBLE);
		tv_right.setText(str);
	}
	
	public void setRightTitleEnable(boolean enabled){
		tv_right.setEnabled(enabled);
	}
	
	public void setrightTitle(String str,int id) {
		// title_right.setVisibility(View.GONE);
		tv_right.setVisibility(View.VISIBLE);
		tv_right.setText(str);
		if (isAdded()){
			Drawable rightDrawable = getResources().getDrawable(id);
			rightDrawable.setBounds(5, 0, rightDrawable.getMinimumWidth(), rightDrawable.getMinimumHeight());
			tv_right.setCompoundDrawables(null, null, rightDrawable, null);
		}
	}

	public void setrightTitleImage(int resId) {
		this.setRightbuttonVisiable(View.VISIBLE);
		title_right.setImageResource(resId);
	}


	public void setRightRedPointVisibility(int visibility){
		mRedPoint.setVisibility(visibility);
	}

	public void setCurrentPoint(int index){
		mPoint1.setVisibility(View.VISIBLE);
		mPoint2.setVisibility(View.VISIBLE);
		mPoint3.setVisibility(View.VISIBLE);

		if(index == 1){
			mPoint1.setImageResource(R.drawable.ic_white_point);
			mPoint2.setImageResource(R.drawable.ic_gray_point);
			mPoint3.setImageResource(R.drawable.ic_gray_point);
		}else if(index == 2){
			mPoint2.setImageResource(R.drawable.ic_white_point);
			mPoint1.setImageResource(R.drawable.ic_gray_point);
			mPoint3.setImageResource(R.drawable.ic_gray_point);
		}else if(index == 3){
			mPoint3.setImageResource(R.drawable.ic_white_point);
			mPoint1.setImageResource(R.drawable.ic_gray_point);
			mPoint2.setImageResource(R.drawable.ic_gray_point);
		}

	}
	
	@Override
	public void onClick(View arg0) {
		super.onClick(arg0);
		
		switch (arg0.getId()) {
		case R.id.title_left2:
			setRoll();
			break;

		default:
			break;
		}
	}

	public void setTitleWhitStyle(){

		getView().setBackgroundColor(Color.WHITE);
		title_left.setCompoundDrawablesWithIntrinsicBounds
				(getResources().getDrawable(R.drawable.reservation_back),null,null,null);
		title_center.setTextColor(getResources().getColor(R.color.color_444444));
	}

}
